package com.codingke.codingkeplayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.codingke.codingkeplayer.R;
import com.codingke.codingkeplayer.utils.MediaUtils;
import com.codingke.codingkeplayer.vo.Mp3Info;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016-2-18.
 */
public class MyMusicListAdapter extends BaseAdapter {

    private Context cxt;
    private ArrayList<Mp3Info> mp3Infos;

    public MyMusicListAdapter(Context cxt, ArrayList<Mp3Info> mp3Infos){
        this.cxt = cxt;
        this.mp3Infos = mp3Infos;
    }

    public void setMp3Infos(ArrayList<Mp3Info> mp3Infos) {
        this.mp3Infos = mp3Infos;
    }

    @Override
    public int getCount() {
        return mp3Infos.size();
    }

    @Override
    public Object getItem(int i) {
        return mp3Infos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null){
            view = LayoutInflater.from(cxt).inflate(R.layout.item_music_list, null);
            vh = new ViewHolder();
            vh.textView1_title = (TextView) view.findViewById(R.id.textView1_title);
            vh.textView2_singer = (TextView) view.findViewById(R.id.textView2_singer);
            vh.textView3_time = (TextView) view.findViewById(R.id.textView3_time);
            view.setTag(vh);
        }

        vh = (ViewHolder) view.getTag();
        Mp3Info mp3Info = mp3Infos.get(i);
        vh.textView1_title.setText(mp3Info.getTitle());
        vh.textView2_singer.setText(mp3Info.getArtist());
        vh.textView3_time.setText(MediaUtils.formatTime(mp3Info.getDuration()));
        return view;
    }

    static class ViewHolder{
        TextView textView1_title;
        TextView textView2_singer;
        TextView textView3_time;
    }
}
